package com.yunji.hushuang.dynamicproxy;

import com.yunji.hushuang.common.IHomeworkService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author hushuang
 * @since 2019/10/14
 */
public class MyInvocationHandler implements InvocationHandler {

    private IHomeworkService target;

    public MyInvocationHandler(IHomeworkService target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        long start = System.currentTimeMillis();
        System.out.println();
        System.out.println("==>>代理哥帮你计算，开始做作业了");

        Object result = method.invoke(target, args);

        long end = System.currentTimeMillis();
        System.out.println(String.format("==>>代理哥帮你计算，你花了 %d ms", end - start));
        return result;
    }

}
